//
//  ImageViewController.m
//  HeartTravel
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ImageViewController.h"
#import "ImageCollectionViewCell.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "ImageDetailViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *imageArr;

@end

@implementation ImageViewController

- (void)dealloc{
    [_imageArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *str = self.titleName;
    self.title = [NSString stringWithFormat:@"%@图片",str];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createView];
    
}

- (void)createData{
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&sign=1024092531695f3d4b5b6f0892f391d3",self.type,self.bId] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        //       解析items数据
        NSArray *itemsArr = dic[@"items"];
        for (NSDictionary *tempDic in itemsArr) {
            [self.imageArr addObject:tempDic[@"photo"]];
        }
        UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1000];
        [collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

//创建视图
- (void)createView{

    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-gezicomfanhui1"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.itemSize = CGSizeMake((WIDTH - 20) / 3, (WIDTH - 20) / 3);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView release];
    [collectionView registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"reuseCell"];
    collectionView.tag = 1000;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.pagingEnabled = YES;
}

//返回按钮方法
- (void)back:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCell" forIndexPath:indexPath];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageDetailViewController *imageDeatailVC = [[ImageDetailViewController alloc] init];
    [self.navigationController pushViewController:imageDeatailVC animated:YES];
    imageDeatailVC.bId = self.bId;
    imageDeatailVC.type = self.type;
    [imageDeatailVC release];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
